package com.ruantu.clothing.controller.wxController;

import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.entity.wxpandian.*;
import com.ruantu.clothing.service.wxpandian.WXPandianService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/wxpandian")
public class WXPandianController {
    @Autowired
    private WXPandianService wxPandianService;

    public WXPandianService getWxPandianService() {
        return wxPandianService;
    }

    public void setWxPandianService(WXPandianService wxPandianService) {
        this.wxPandianService = wxPandianService;
    }

    /**
     * 通过编号查询商品详情
     * @param product_code
     * @return
     */
    @RequestMapping("selectPdByCode.do")
    @ResponseBody
    public ReseltMap_A selectPdByCode(String product_code,HttpSession session){
        //HttpSession session = request.getSession();
        Object obj = session.getAttribute("info");
        /*if (obj!=null&&""!=obj){
            User user=(User) obj;
        }*/
        /*try {
            User user=(User) obj;
            String jg_id=user.getJgid();
        return  wxPandianService.selectPdByCode(product_code,jg_id);
        }catch (Exception e){
            String erromsg="101";
            return
        }*/
        User user=(User) obj;
        String jg_id=user.getJgid();
        return  wxPandianService.selectPdByCode(product_code,jg_id);

    }
    /**
     * 根据机构获取最新盘点
     * @return
     */
    @RequestMapping("selectNewPandian.do")
    @ResponseBody
    public Pandian selectNewPandian(HttpSession session){
        Object obj = session.getAttribute("info");
        User user=(User) obj;
        String jg_id=user.getJgid();
        return wxPandianService.selectNewPandian(jg_id);
    }

    /**
     * 获取根据产品条码和机构获取storeID
     * @param product_code 产品条码
     * @return 最新盘点
     */
    @RequestMapping("selectStoreIdByCode.do")
    @ResponseBody
    public String selectStoreIdByCode(String product_code,HttpSession session){
        Object obj = session.getAttribute("info");
        User user=(User) obj;
        String jg_id=user.getJgid();
        return wxPandianService.selectStoreIdByCode(product_code,jg_id);
    };

    /**
     * 新增盘点子表记录
     * @param store_id storeID
     * @param shuliang  盘点后数量
     * @param //pandianren 盘点人
     * @param pandian_id 盘点ID
     * @param yk    盈亏
     */
    //void insertJLtoPdChild (String store_id,Integer shuliang,String pandianren,String pandian_id,Integer yk);
    @RequestMapping("insertJLtoPdChild.do")
    @ResponseBody
    public void insertJLtoPdChild (String store_id,Integer shuliang,String pandian_id,Integer yk,HttpSession session){
        Object obj = session.getAttribute("info");
        User user=(User) obj;
        Integer pandianren=user.getId();
        wxPandianService.insertJLtoPdChild(store_id,shuliang,pandianren,pandian_id,yk);
    };

    /**
     *查询此次（最新）盘点记录详情
     * @param selectText 条码
     * @param index 页码
     * @param size 条数
     * @return
     */
    @RequestMapping("selsctNewPDXQ.do")
    @ResponseBody
    public List<ResultMap_PDJLXQ> selsctNewPDXQ (String selectText, Integer index, Integer size,HttpSession session){
        //product_code=null;
        Object obj = session.getAttribute("info");
        User user=(User) obj;
        String jg_id=user.getJgid();
        return wxPandianService.selsctNewPDXQ(selectText,jg_id,index,size);
    }

    /**
     * 查询此次（最新）盘点记录数量
     * @param selectText 搜索条件
     * @return
     */
    @RequestMapping("selsctNewPDXQCount.do")
    @ResponseBody
    public Integer selsctNewPDXQCount(String selectText,Integer size,HttpSession session){
        Object obj = session.getAttribute("info");
        User user=(User) obj;
        String jg_id=user.getJgid();
        return wxPandianService.selsctNewPDXQCount(selectText,jg_id,size);
    }
    /**
     * 修改此次盘点的商品库存
     * @param update_shuliang 修改后的数量
     * @param pdzb_id   需要修改的盘点子表
     */
    @RequestMapping("updatePDZBShuling.do")
    @ResponseBody
    public String updatePDZBShuling(Integer update_shuliang,Integer yk,String pdzb_id){
        try{
            wxPandianService.updatePDZB_shuling(update_shuliang,yk,pdzb_id);
            return "成功";
        }catch (Exception e){
            return "失败";
        }
    }
    /**
     * 根据款号/编号查询产品
     * @param selectText 搜索条件
     * @return
     */
    @RequestMapping("selectProductBys.do")
    @ResponseBody
    public List<PdBySelect> selectProductBys(String selectText,Integer index,Integer size,HttpSession session){
        Object obj = session.getAttribute("info");
        User user=(User) obj;
        String jg_id=user.getJgid();
        return wxPandianService.selectProductBys(selectText,jg_id,index,size);
    }
    /**
     * 获取根据款号/编号查询的产品的总条数
     * @param selectText 搜索条件
     * @return 总条数
     */
    @RequestMapping("selectTotalpageByCount.do")
    @ResponseBody
    public Integer selectTotalpageByCount(String selectText,Integer size,HttpSession session){
        Object obj = session.getAttribute("info");
        User user=(User) obj;
        String jg_id=user.getJgid();
        return wxPandianService.selectTotalpageByCount(selectText,jg_id,size);
    }

    /**
     * 盘点子表查重
     * @param store_id
     * @param pandian_id
     * @return
     */
    @RequestMapping("selectRepetition.do")
    @ResponseBody
    public ModResult selectRepetition(String store_id, String pandian_id){
        return wxPandianService.selectRepetition(store_id,pandian_id);
    }
}
